import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService ,BeActionService} from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';

@Injectable()
export class DjpzxzFrmControllerService {
  private frameCode = "oprm";
  private frameId = "djpzxz-" + this.frameCode + "-component_djpzxz_data-grid-component";
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) {}

  /**
   * 添加参数
   * @remarks 
   * @returns 
   */
  addparam(): any {
    let mainName =  this.frameContext.viewModel.uiState['mainName']
    let className =  this.frameContext.viewModel.uiState['className']
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'addparam';
    const body = {
      mainName:mainName,
      className:className,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {})
    ) 
  }

  /**
   * 确认点击事件
   * @remarks 
   * @returns 
   */
  confirm(): any {
      console.log('viewInitEvent:', this.frameContext.appContext.frameContextManager);
      const uiState = this.frameContext.uiState;
      debugger
      const checkeds = this.frameContext.appContext.componentManager.get([this.frameId, 'dataGrid']).checkeds;
      const mainids = this.frameContext.appContext.componentManager.get(['data-grid-component', 'dataGrid']).checkeds;
      let ids: string[] = [];
      for(let item of mainids){
        ids.push(item.id);
      }
      // console.log('checkeds:', checkeds);
      // const mainids = this.frameContext.viewModel.uiState['mainids'];
      // const arr = mainids.split(",");
      debugger;
      if(checkeds.length == 0) {
        this.messageService.warning('请选择一条数据！');
        return EMPTY;
      }
      let datas: string[] = [];
      for(let item of checkeds){
        datas.push(item.data);
      }
      //后台赋值
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'xzdatas';
    const body = {
      ids: ids,
      data: datas,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        this.messageService.info("关联点检配置成功!");
      })
    )
  }
}
